package com.site.service.impl.dining;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.site.dao.dining.NewsSortDao;
import com.site.dto.dining.NewsSortBizBean;
import com.site.model.dining.NewsSortInfo;
import com.site.service.dining.NewsSortService;
import com.site.util.PropertyUtils;
import com.site.util.exception.ApplicationException;
import com.site.util.exception.SystemException;

@Service
public class NewsSortServiceImpl implements NewsSortService {

	private NewsSortDao dao;

	@Resource
	public void setDao(NewsSortDao dao) {
		this.dao = dao;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Object execute(Object obj, String command) throws SystemException,
			ApplicationException {
		// 删除
		if (COMMAND_DELETE.equals(command)) {
			delete((Map<String, Object>) obj);
			// 增加
		} else if (COMMAND_ADD.equals(command)) {
			add((Map<String, Object>) obj);
			// 修改
		} else if (COMMAND_UPDATE.equals(command)) {
			eidt((Map<String, Object>) obj);
		}
		return null;
	}

	private void eidt(Map<String, Object> map) {
		if (null != map && !map.isEmpty()) {
			NewsSortBizBean newsSortBizBean = (NewsSortBizBean) map
					.get("sortbean");
			NewsSortInfo newsSortInfo = dao.getEntityById(NewsSortInfo.class,
					newsSortBizBean.getId());
			PropertyUtils.copyNotNullProperties(newsSortInfo, newsSortBizBean);
			dao.update(newsSortInfo);
		}
	}

	private void add(Map<String, Object> map) {
		if (map != null && !map.isEmpty()) {
			NewsSortBizBean newsSortBizBean = (NewsSortBizBean) map
					.get("sortbean");
			NewsSortInfo newsSortInfo = new NewsSortInfo();
			PropertyUtils.copyProperties(newsSortInfo, newsSortBizBean);
			dao.save(newsSortInfo);
		}
	}

	private void delete(Map<String, Object> map) {
		if (null != map && !map.isEmpty()) {
			NewsSortBizBean newsSortBizBean = (NewsSortBizBean) map
					.get("sortbean");
			NewsSortInfo newsSortInfo = dao.getEntityById(NewsSortInfo.class,
					newsSortBizBean.getId());
			// 删除相应的图片
			if (null != newsSortInfo) {
				dao.delete(NewsSortInfo.class, newsSortInfo.getId());
			}
		}
	}

	@Override
	public List<NewsSortBizBean> getNewsSorts() {
		List<NewsSortInfo> list = dao.GetNewsSorts();
		if (list != null && !list.isEmpty()) {
			List<NewsSortBizBean> news = new ArrayList<NewsSortBizBean>();
			for (NewsSortInfo NewsSortInfo : list) {
				NewsSortBizBean news2 = new NewsSortBizBean();
				PropertyUtils.copyProperties(news2, NewsSortInfo);
				news.add(news2);
			}
			return news;
		}
		return null;
	}

	@Override
	public NewsSortBizBean getEntityById(Long id) {
		NewsSortInfo newsSortInfo = dao.getEntityById(NewsSortInfo.class, id);
		if (null != newsSortInfo) {
			NewsSortBizBean newsSortBizBean = new NewsSortBizBean();
			PropertyUtils.copyProperties(newsSortBizBean, newsSortInfo);
			return newsSortBizBean;
		}
		return null;
	}
}
